package artid.com.arttogoa;

import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.AppItem;
import artid.com.arttogoa.util.AlertUtils;
import artid.com.arttogoa.util.DataCleanManager_Util;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：SettingActivity   
 * 类描述：设置   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午2:35:41      
 *
 */
public class SettingActivity extends BaseActivity implements OnClickListener {
	private RelativeLayout mNewMessage, mUpdatePass, mClearCache, mAboutUs,
	mOutLogin;
	private Intent mIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		setContent(this, getIntent());
		setBack();
		mContext=this;
		ArtWorkApp.getInstance().addActivity(this);
		mNewMessage = (RelativeLayout) findViewById(R.id.new_message);
		mUpdatePass = (RelativeLayout) findViewById(R.id.update_pass);
		mClearCache = (RelativeLayout) findViewById(R.id.clear_cache);
		mAboutUs = (RelativeLayout) findViewById(R.id.about_us);
		mOutLogin = (RelativeLayout) findViewById(R.id.out_login);
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("设置");
		setBack();
		mNewMessage.setOnClickListener(this);
		mUpdatePass.setOnClickListener(this);
		mClearCache.setOnClickListener(this);
		mAboutUs.setOnClickListener(this);
		mOutLogin.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.new_message:// 新消息通知
			mIntent = new Intent(SettingActivity.this, NewMessageActivity.class);
			startActivity(mIntent);
			break;
		case R.id.update_pass:// 修改密码
			mIntent = new Intent(SettingActivity.this, UpdatePassActivity.class);
			startActivity(mIntent);
			break;
		case R.id.clear_cache:// 清理缓存
			//清除缓存
			VolleyBasic.showLoding(mContext);
			showToast("正在清理缓存，请稍等....");
			try {
				DataCleanManager_Util.clearAllCache(SettingActivity.this);
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						VolleyBasic.disMissLoding();
						showToast("缓存清理成功");
					}
				}, (int)(Math.random() * 10+1)*1000);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.about_us:// 关于我们
			mIntent = new Intent(SettingActivity.this, AboutUsActivity.class);
			startActivity(mIntent);
			finish();
			break;
		case R.id.out_login:// 退出登录
			AlertUtils.deleteDialog(mContext,"是否退出程序",Integer.parseInt(NetUtils.getDisplayMetrics(this).get(NetUtils.DM_WIDTH)), new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertUtils.dismiss();
					logout();
				}
			});
			break;

		default:
			break;
		}

	}

	/**
	 * 推出登陆
	 */
	private void logout() {
		VolleyBasic.getInstance(SettingActivity.this).post(Cache.logout,
				VolleyMap.getInstance().logout(), AppItem.class,
				new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				mIntent = new Intent(SettingActivity.this,LoginAvtivity.class);
				startActivity(mIntent);
				ArtWorkApp.getInstance().exit();
				MySharedPreferences.getMySharedPreferences(mContext).setValue("PassWord", "");
			}

			@Override
			public void onError(VolleyError volleyError) {

			}
		});
	}
}
